// pages/drawPrize/demo2/demo2.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //声明列表进行存储信息
    bigwhellists:[],
    //用来存储图片的列表
    imagelists:[],
    //中奖信息，用来存储中奖后的信息
    zhongxin:'',
    //中奖信息，用来存储中奖后的图片
    zhongimage:'',
    // 存储奖品的使用条件
    zhongcondition:'',
    //转盘图片
    lightboo:"/images/cj/zhuanpan.png",
    // 中奖框显示
    zhongboo:true,
      zpData:{
          //总块数
          equalParts:6, 
          //每块所占度数
          oneAngle:60, 
      },
      //判断转盘是否还在转动
      ifRoate:false, 
      //旋转的角度
      zpRotateDeg:'', 
      //总块数
      curKey:6, 
      //是否中奖
      ifWinning:false, 
  },
  selectCity() {
      wx.navigateTo({
          url: `plugin://city-choice/city-choice?onHistory=${true}&onDarkMode=${this.data.isDark}&hotCitys=${this.data.hotCitys}`
      })
  },
  // 转盘数据初始化
  setZpDefault(){
      let equalPartsNum = this.data.zpData.awardSetting.length;
      let oneAngleNum = 360 / equalPartsNum;
      let equalParts = 'zpData.equalParts';
      let oneAngle = 'zpData.oneAngle';
      this.setData({
          //总块数
          [equalParts] : equalPartsNum,
          // 每块对应的角度
          [oneAngle] : oneAngleNum,
      });
  },
  // 旋转效果
  setToRotate(degNum){
      this.setData({
          zpRotateDeg : '-webkit-transform: rotate(' + degNum + 'deg);transform: rotate(' + degNum + 'deg);-webkit-transition: all 5s ease;transition: all 5s ease;',
      });
  },
  //根据 设置的 指针停止时指向的格子（中奖结果），设置其旋转角度区间
  //awardSettingNumber  取值范围 1 至 总块数
  setRotate(awardSettingNumber){ 
      setTimeout(() => {
          //指针指向的最小角度
          let minAngle = 360 - awardSettingNumber * this.data.zpData.oneAngle + 5;
          //指针指向的最大角度
          let maxAngle = 360 - (awardSettingNumber - 1) * this.data.zpData.oneAngle - 5;
          //旋转的块
          let newAngle = Math.floor(minAngle + Math.random() * (maxAngle - minAngle)) + 360 * 15;
          this.setToRotate(newAngle);
          this.setData({
            lightboo:"/images/cj/zhuanpan.gif"
          })
          setTimeout(() => {
            this.roateEnd(awardSettingNumber);
            this.setData({
              lightboo:"/images/cj/zhuanpan.png",
              zhongboo:false
            })
        }, 5150);
      },50);
  },
  //旋转结束后执行
  roateEnd(awardSettingNumber){
      console.log('中奖指向的区域 -> ' + awardSettingNumber , this.data.curKey);
      console.log('中奖获得的奖品名称 -> ' + this.data.zpData.awardSetting[awardSettingNumber - 1] , this.data.zpData.awardSetting[this.data.curKey - 1]);
      // 是否中奖，这里如果你的未中奖信息不是感谢参与，那么你可以进行修改
      if(this.data.zpData.awardSetting[awardSettingNumber-1] != '感谢参与'){
          console.log('中奖');
          this.setData({
            ifWinning:true,
            zhongxin:this.data.zpData.awardSetting[awardSettingNumber-1],
            zhongimage:this.data.imagelists[awardSettingNumber-1],
            zhongcondition:this.data.conditions[awardSettingNumber-1]
          })
      }else{
          console.log('未中奖');
          this.setData({
            ifWinning:false,
            zhongxin:this.data.zpData.awardSetting[awardSettingNumber-1],
            zhongimage:this.data.imagelists[awardSettingNumber-1]
          })
      }
      setTimeout(() => {
          this.setData({
            //判断转盘是否还在转动
              ifRoate : false, 
          });
      }, 100);
  },
  //点击抽奖
  getLucky(){
      if(this.data.ifRoate){
          return false;
      }  
      this.setData({
        //判断转盘是否还在转动
          ifRoate : true, 
          zpRotateDeg : ''
      });        
      // 获取抽奖结果
      var res = {
          status:1,
          //抽奖结果 ： 取值范围 1 到 总块数
          curKey:Math.floor(1 + Math.random() * this.data.zpData.equalParts), 
      }
          if(res.status == 1){
              this.setData({
                  curKey : res.curKey,
                  ifWinning : res.ifWinning
              }); 
              this.setRotate(this.data.curKey);
          }else{
              this.setData({
                //判断转盘是否还在转动
                  ifRoate : false, 
              });
              wx.showModal({
                  title: '提示',
                  showCancel:false,
                  content: res.info,
              })
          }
  },
  // 点击立即领取后执行的方法
  zhongjiang(){
    // 点击领取后将中奖框隐藏，你也可以在此做其它操作
    this.setData({
      zhongboo:true
    })
  },
  //初始化数据请求
  initData(){
    let zpImg = 'zpData.zpImg';
    let awardSetting = 'zpData.awardSetting';
    var that = this
    wx.request({
      //写你的对应接口
      url: '',
      method:"",
      success(e){
        var list = e.data.data
        that.setData({
          bigwhellists:list,
          [awardSetting]:[
            //给列表赋值
            "图形1","图形2","图形3","图形4","图形5","感谢参与",
              // list[0].amount+list[0].prizetype,
              // list[1].amount+list[1].prizetype,
              // list[2].amount+list[2].prizetype,
              // list[3].amount+list[3].prizetype,
              // list[4].amount+list[4].prizetype,
              // list[5].amount+list[5].prizetype,
          ],
          // 给图片赋值
          imagelists:[
            'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
            'https://img.alicdn.com/bao/uploaded/i3/1669409267/TB2Alo8dTmWBKNjSZFBXXXxUFXa_!!1669409267.jpg_b.jpg',
            'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
            'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
            'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
            'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
          ],
          //奖品对应的使用条件
          conditions:[
            "中奖1","中奖2","中奖3","中奖4","中奖5","中奖6",
          ]
        });
      that.setZpDefault();
     },
     fail(){
       console.log("获取转盘数据失败")
     }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   * 并通过接口获取转盘数据
   */
  onLoad: function (options) {
   this.setData({
    bigwhellists:6,
    'zpData.awardSetting':[
      //给列表赋值
      "图形1","图形2","图形3","图形4","图形5","感谢参与",
    ],
    // 给图片赋值
    imagelists:[
      'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
      'https://img.alicdn.com/bao/uploaded/i3/1669409267/TB2Alo8dTmWBKNjSZFBXXXxUFXa_!!1669409267.jpg_b.jpg',
      'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
      'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
      'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
      'https://img.alicdn.com/bao/uploaded/i7/TB1TZgmGFXXXXb1XFXXxWT0.VXX_082827.jpg_b.jpg',
    ],
    //奖品对应的使用条件
    conditions:[
      "中奖1","中奖2","中奖3","中奖4","中奖5","中奖6",
    ]
  });
  this.setZpDefault();
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  }
})